package com.szcinda.fitness.persistence;

import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
@Entity
public class DailyTrain extends BaseEntity {
    private LocalDate trainDate;
    private String openId;//作为学员的唯一标识
    private String trainNote;
    private String trainPositions;//多个训练部位以英文逗号隔开
    private String trainTypes;//多个训练类型以英文逗号隔开

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "dailyTrain", orphanRemoval = true)
    private List<TrainMovement> trainMovements = new ArrayList<>();

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "dailyTrain", orphanRemoval = true)
    private List<BodyMeasurement> bodyMeasurements = new ArrayList<>();

    public void addMovements(List<TrainMovement> trainMovements){
        this.trainMovements.clear();
        trainMovements.forEach(item->{
            item.setDailyTrain(this);
            this.trainMovements.add(item);
        });
    }
    public void setBodyMeasurements(List<BodyMeasurement> bodyMeasurements){
        this.bodyMeasurements.clear();
        bodyMeasurements.forEach(item->{
            item.setDailyTrain(this);
            this.bodyMeasurements.add(item);
        });
    }
}
